import json
import os
import requests
from configparser import RawConfigParser

config = RawConfigParser()
if os.path.exists('./Static/Config.ini'):
    config.read('./Static/Config.ini', encoding='utf-8')
else:
    config.read('../Static/Config.ini', encoding='utf-8')

jira_headers = {'Content-Type': 'application/json',
                'Authorization': 'Basic {}'.format(config.get('jira', 'headers_Basic'))}
pwd = os.path.abspath(os.path.join(os.getcwd()))
log_format = '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'


def feishu_header():
    url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/'
    bot_app = {
        "app_id": config.get('feishu', 'app_id'),
        "app_secret": config.get('feishu', 'app_secret')
    }

    token = requests.post(url, data=json.dumps(bot_app))
    token_key = json.loads(token.text)
    header_dir = {
        "content-type": "application/json; charset=utf-8",
        "Authorization": "Bearer " + str(token_key["tenant_access_token"])
    }

    return header_dir


def feishu_header_select():
    url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/'
    bot_app = {
        "app_id": config.get('feishu', 'app_id_select'),
        "app_secret": config.get('feishu', 'app_secret_select')
    }

    token = requests.post(url, data=json.dumps(bot_app))
    token_key = json.loads(token.text)
    header_dir = {
        "content-type": "application/json; charset=utf-8",
        "Authorization": "Bearer " + str(token_key["tenant_access_token"])
    }

    return header_dir
